﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;

namespace ConfigCreate.Controls
{
    /// <summary>
    /// CreateControlOpenFile.xaml 的交互逻辑
    /// </summary>
    public partial class CreateControlOpenFile : System.Windows.Controls.UserControl
    {
        /// <summary>
        /// 选择项
        /// </summary>
        public ObservableCollection<string> DataList { get; set; } = new ObservableCollection<string>();

        /// <summary>
        /// 选择文件通知
        /// </summary>
        public event EventHandler<ObservableCollection<string>>? SelectedFileChanged;

        /// <summary>
        /// 
        /// </summary>
        public CreateControlOpenFile()
        {
            InitializeComponent();
            tagCon.AddHandler(HandyControl.Controls.Tag.ClosedEvent, new RoutedEventHandler(Tag_OnClosed));
        }

        private void Tag_OnClosed(object sender, RoutedEventArgs e)
        {
            SelectedFileChanged?.Invoke(this, DataList);
            if (DataList.Count > 0)
            {
                SelectedFileName = DataList[DataList.Count - 1];
            }
            else
            {
                SelectedFileName = string.Empty;
                ShowBox = false;
            }
        }

        #region 属性
        /// <summary>
        /// 选择文件路径
        /// </summary>
        public string SelectedFileName
        {
            get { return (string)GetValue(SelectedFileNameProperty); }
            set { SetValue(SelectedFileNameProperty, value); }
        }
        /// <summary>
        /// 选择文件路径
        /// </summary>
        public static readonly DependencyProperty SelectedFileNameProperty = DependencyProperty.Register("SelectedFileName", typeof(string), typeof(CreateControlOpenFile), new PropertyMetadata(string.Empty));

        /// <summary>
        /// 对话框标题
        /// </summary>
        public string DialogTitle
        {
            get { return (string)GetValue(DialogTitleProperty); }
            set { SetValue(DialogTitleProperty, value); }
        }
        /// <summary>
        /// 对话框标题
        /// </summary>
        public static readonly DependencyProperty DialogTitleProperty = DependencyProperty.Register("DialogTitle", typeof(string), typeof(CreateControlOpenFile), new PropertyMetadata("请选择文件"));

        /// <summary>
        /// 对话框过滤
        /// </summary>
        public string DialogFilter
        {
            get { return (string)GetValue(DialogFilterProperty); }
            set { SetValue(DialogFilterProperty, value); }
        }
        /// <summary>
        /// 对话框过滤
        /// </summary>
        public static readonly DependencyProperty DialogFilterProperty = DependencyProperty.Register("DialogFilter", typeof(string), typeof(CreateControlOpenFile), new PropertyMetadata("所有文件|*.*"));

        /// <summary>
        /// 是否可多选文件
        /// </summary>
        public bool Multiselect
        {
            get { return (bool)GetValue(MultiselectProperty); }
            set { SetValue(MultiselectProperty, value); }
        }
        /// <summary>
        /// 是否可多选文件
        /// </summary>
        public static readonly DependencyProperty MultiselectProperty = DependencyProperty.Register("Multiselect", typeof(bool), typeof(CreateControlOpenFile), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsMeasure, (d, e) =>
        {
            bool flag = (bool)e.NewValue;
            if (flag)
            {
                ((CreateControlOpenFile)d).IsReadOnly = true;
            }
        }));

        /// <summary>
        /// 其中文本框是否为只读模式
        /// </summary>
        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }
        /// <summary>
        /// 其中文本框是否为只读模式
        /// </summary>
        public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(CreateControlOpenFile), new PropertyMetadata(false));

        internal bool ShowBox
        {
            get { return (bool)(GetValue(ShowBoxProperty)); }
            set { SetValue(ShowBoxProperty, value); }
        }
        internal static readonly DependencyProperty ShowBoxProperty = DependencyProperty.Register("ShowBox", typeof(bool), typeof(CreateControlOpenFile), new PropertyMetadata(false));


        #endregion

        private void OpenFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Title = DialogTitle;
            of.Filter= DialogFilter;
            if (!string.IsNullOrEmpty(SelectedFileName))
            {
                FileInfo finfo = new FileInfo(SelectedFileName);
                if (finfo.Directory?.Exists == true)
                    of.InitialDirectory = finfo.DirectoryName;
            }
            of.Multiselect = Multiselect;
            if (DataList.Count > 0)
                of.FileName = DataList[DataList.Count - 1];
            if (of.ShowDialog() == true)
            {
                DataList.Clear();
                if (Multiselect)
                {
                    foreach (var file in of.FileNames)
                        DataList.Add(file);
                    SelectedFileName = DataList[DataList.Count - 1];
                    ShowBox = true;
                }
                else
                {
                    DataList.Add(of.FileName);
                    SelectedFileName = of.FileName;
                }
                SelectedFileChanged?.Invoke(this, DataList);
            }
        }
    }
}
